package com.qk.controller;

import com.qk.common.Result;
import com.qk.utils.AliyunOSSOperator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

@RestController
@Slf4j
public class UploadController {


    @Autowired
    private AliyunOSSOperator aliyunOSSOperator;
    @PostMapping("/upload")
    public Result upload(MultipartFile image) throws Exception {
        log.info("上传图片:{}",image);
        //构造一个文件名(不能重复) - > 使用uuid:
        String fileName = image.getOriginalFilename();  //获取原始的文件名  1.png
        String extName = fileName.substring(fileName.lastIndexOf(".")); //截取文件名中的后缀名  .png
        String newFileName = UUID.randomUUID().toString() + extName;   //随机获取一个文件名  再拼接后缀

        String url = aliyunOSSOperator.upload(image.getBytes(), newFileName);
        return Result.success(url);
    }
}
